import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { catchError, map, tap } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { of } from 'rxjs/observable/of';

@Injectable()
export class ChatProvider {

  constructor(public http: HttpClient) { }

  public getAnswer(step?: string) {
    const url = 'assets/mock/chat.content.json';
    return this.http.get(url)
    .pipe(
      tap(_ => this.log('get answer')),
      catchError(this.handleError('error: getAnswer', []))
    )
  }

  /** Log a HeroService message with the MessageService */
  private log(message: string) {
    console.log(`chat: ${message}`);
  }

  private handleError<T> (operation = 'operation', result?: T) {
    console.log('---handle error---');
    return (error: any): Observable<T> => {
   
      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead
   
      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }
}
